import * as bm from "/js/baseModule.js";

export class Module extends bm.baseModule {
    constructor(view) {
        super(view);
        let that = this;

        //套餐详情渲染
        let mypack = globalData.pack;
        let userinfo = globalData.user;
        this.packInfo = new Vue({
            el: '#ValueAddService_vue',
            data: {
                pack: mypack,
                user: userinfo
            },
            computed: {
                expired: function () {
                    if (globalData.pack.id == 0) {
                        return true
                    }
                    let now = new Date().getTime();
                    let endDate = new Date(globalData.pack.end_date).getTime();
                    let dayTransMs = 1000 * 60 * 60 * 24;//上一行代码得出的结果是截止日期当天的0点0分，所以需要在此基础上加24小时；
                    return now > endDate + dayTransMs
                },
                percentCall: function () {
                    if (this.$options.computed.expired()) {
                        return 0;
                    }
                    return this.pack.call_point / this.pack.accum_call_point * 100
                },
                percentBid: function () {
                    if (this.$options.computed.expired()) {
                        return 0;
                    }
                    //如果剩余汇币值为负数，进度条设为0
                    if (this.pack.bid_point <= 0) {
                        return 0;
                    }
                    return this.pack.bid_point / this.pack.accum_bid_point * 100
                },
                percentSms: function () {
                    if (this.$options.computed.expired()) {
                        return 0;
                    }
                    return this.pack.sms_point / this.pack.accum_sms_point * 100
                }
            },
            methods: {
                endTime: function (endTime) {
                    let data = "";
                    if (endTime == "") {
                        return data;
                    }
                    let time = endTime.toString().split('-');
                    if (time[1].substr(0, 1) == 0) {
                        time[1] = time[1].substr(1, 2);
                    }
                    if (time[2].substr(0, 1) == 0 && time[2].substr(1, 2) != 0) {
                        time[2] = time[2].substr(1, 2);
                    }
                    data = time[0] + "年" + time[1] + "月" + time[2] + "日"
                    return data;
                },
            }
        });
        //获取我的套餐信息
        api.getMyPack();
    }

    /** 购买套餐 */
    _evt_openAlipay(obj) {
        let type = $(obj).attr('type');
        let amt = $(obj).attr('amt');
        /**
         * 微信支付
         */
        api.postForWxPay(type, function (json) {
            function onBridgeReady() {
                WeixinJSBridge.invoke(
                    'getBrandWCPayRequest', json.wxpay,
                    function (res) {
                        // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回"ok"，但并不保证它绝对可靠。
                        if (res.err_msg == "get_brand_wcpay_request:ok") {
                            api.getMyPack(function (data) {
                                globalData.pack.id = data.pack.id;
                                globalData.pack.accum_call_point = data.pack.accum_call_point;
                                globalData.pack.accum_sms_point = data.pack.accum_sms_point;
                                globalData.pack.accum_bid_point = data.pack.accum_bid_point;
                                globalData.pack.bid_point = data.pack.bid_point;
                                globalData.pack.call_point = data.pack.call_point;
                                globalData.pack.sms_point = data.pack.sms_point;
                                globalData.pack.end_date = data.pack.end_date;
                                globalData.pack.prov_list = data.pack.prov_list;
                                // funs.tip('购买成功', '', 'succ');
                            });
                        } else {
                            //支付不成功
                            // funs.tip('支付失败', res.err_msg, 'fail');
                        }
                    }
                );
            }
            if (typeof WeixinJSBridge == "undefined") {
                if (document.addEventListener) {
                    document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                } else if (document.attachEvent) {
                    document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                    document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                }
            } else {
                onBridgeReady();
            }
        }, function (error) {
            if (error.name == '41009002') {
                funs.tip(error.note, "请先去设置服务资料，成为工程商", "fail");
            } else {
                funs.tip(error.note, "", "fail");
            }

        })
    }
    /**什么是汇币*/
    _evt_openRule() {
        // tomato.getVPresenter<tomato.VPresenter>(funs.moduleToUrl('m/project/NewGrabRuleModel'), (NewGrabRuleModel) => {
        //     tdom.open(NewGrabRuleModel, tdom.DialogTarget.Blank, {
        //         size: { width: '90%', height: tomato.DialogSize.Content },
        //         effect: tdom.DialogEffect.scale,
        //         masked: true
        //     });
        // });
    }
    /**老用户优惠*/
    _evt_openGrabRecharge() {
        // tomato.getVPresenter<tomato.VPresenter>(funs.moduleToUrl('m/grab/GrabRechargeModal'), (GrabStatusModal) => {
        //     tdom.open(GrabStatusModal, tdom.DialogTarget.Blank, {
        //         size: { width: '90%', height: tomato.DialogSize.Content },
        //         effect: tdom.DialogEffect.scale,
        //         masked: true,
        //     });
        // });
    }
}